<template>
  <div class="info">
    <template>
      <el-skeleton :loading="loading" />
    </template>
    <template v-if="!loading">

      <el-form inline>
        <el-form-item>
          <el-button type="success" icon="el-icon-folder-add" @click="openWin()">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="infoList" border row-key="id">
        <el-table-column label="编号" prop="id"/>
        <el-table-column label="部门名" prop="fullname"/>
        <el-table-column label="排序值" prop="num"/>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="openWin(scope.row)">修改</el-button>
            <el-button type="danger" size="small" @click="delInfo(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <DeptEdit ref="deptEdit" @reloadTable="getList"/>
  </div>
</template>

<script>
import DeptEdit from "./DeptEdit";
export default {
  name: 'Dept',
  data () {
    return {
      loading: true,
      infoList: [],
    }
  },
  components: { DeptEdit },
  methods: {
    getList () {
      this.$http.get('dept/list').then(({ data }) => {
        this.loading = false
        if (data.code === 20000) {
          this.infoList = data.data
        }
      })
    },

    openWin (info) {
      this.$refs.deptEdit.init(info)
    },
    delInfo (id) {
      this.$confirm('确定删除该数据吗？', '询问').then(() => {
        this.$axios.delete('dept', { params: { id } }).then(({ data }) => {
          if (data.code === 20000) {
            this.$message({
              message: '删除成功',
              type: 'success',
              onClose: () => {
                this.getList()
              }
            })
          } else {
            this.$alert('删除失败！', { type: 'error' })
          }
        })
      })
    }
  },
  created () {
    this.$nextTick(() => {
      this.getList()
    })
  }
}
</script>

<style scoped>

</style>
